﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Timers;
using System.Web.Script.Serialization;

namespace ParkingService_REST_JSON_WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class ParkingService : IParkingService
    {
        private static string response;
        private static int timesAutoUpdated;
        private static DateTime lastUpdate;


        public ParkingService()
        {
            //trådsikres
            Timer t = new Timer(5000); // 1 sec = 1000, 60 sec = 60000

            t.AutoReset = true;

            t.Elapsed += t_Elapsed;

            t.Start();
            
        }

        
        private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                response = new WebClient().DownloadString("http://ucn-parking.herokuapp.com/places.json");
                lastUpdate = DateTime.Now;
                timesAutoUpdated++;

            }
            catch (WebException wex)
            {
                response = wex.Message;
            }

            

        }
        public static DateTime LastUpdate
        {
            get { return lastUpdate; }
            set { lastUpdate = value; }
        }

        public static int TimesAutoUpdated
        {
            get { return timesAutoUpdated; }
            set { timesAutoUpdated = value; }
        }

        

        public string GetJSON(string email)
        {
            WebClient client = new WebClient();
            string result;
            try
            {

                response = new WebClient().DownloadString("http://ucn-parking.herokuapp.com/places.json");
                lastUpdate = DateTime.Now;
                SendEmail(email,response);
            }
            catch (WebException wex)
            {
                response = "SERVER DOWN: " + response;
                SendEmail(email, response);
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            return response;// + "Last_Update: " + lastUpdate.ToString("MM/dd/yy H:mm:ss zzz");
        }

        private void SendEmail(string email, string data)
        {
            var fromAddress = new MailAddress("kingofdebugging@gmail.com", "Parking-Service");
            var toAddress = new MailAddress(email, email);
            const string fromPassword = "debug123";
            const string subject = "Parkeringspladser";
            string body = data;

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                IsBodyHtml = false, // skal sættes til false senere
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }
    }
}
